<?php
/**[前后台公用控制器]
 * @Author: chen
 * @Date:   2014-08-17 18:00:43
 * @Last Modified by:   Administrator
 * @Last Modified time: 2015-03-16 09:14:52
 */
class ExtendAction extends Action {
   /**
   * [verify 验证码]
   * @return [type] [description]
   */
	public function verify()
	{
		import('Class.Code',APP_PATH);
    	$code = new Code();
    	$code->show();
	}


  /**
   * [keditor_upload 编辑器图片上传]
   * @return [type] [description]
   */
  public function keditor_upload()
  {
      // 上传类
      import('ORG.Net.UploadFile');
      $upload = new UploadFile();// 实例化上传类
      $upload->maxSize  = 314572800 ;// 设置附件上传大小
      $ext = explode('|', C('cfg_file'));
      $upload->allowExts  = $ext;// 设置附件上传类型
      $dir = I('get.dir');
      $savePath = './Data/Uploads/'.$dir.'/'.date('Y').'/'.date('m').'/'.date('d').'/';
      $upload->savePath = $savePath;
      // 创建目录
      //is_dir($savePath) || mkdir($savePath,0777,true);
      // 执行上传
      if(!$info=$upload->uploadOne($_FILES['imgFile'],$savePath))
      {
        // 上传错误提示错误信息
        echo json_encode(array('error' => 1, 'message' =>"1".$upload->getErrorMsg()));
      }
      else
      {
        // 上传成功 获取上传文件信息
        
        $fileUrl = $info[0]['savepath'].$info[0]['savename'];
        $keditor = pathinfo($fileUrl);
        // 保存数据到数据库
        $data=array(
          'name'=>$keditor['filename'],
          'ext'=>$keditor['extension'],
          'path'=>$keditor['dirname'],
          'size'=>filesize($fileUrl),
          'addtime'=>time(),
          'user_uid'=>session('uid'),
        );
        $id = D('Upload')->add($data);
        $_SESSION['keditor'][]=$id;
        $fullPath = __ROOT__ . '/' . $fileUrl;
        echo json_encode(array('error' => 0, 'url' => $fullPath));
      }
      exit;
  }

  /**
   * [down 下载]
   * @return [type] [description]
   */
  public function down()
  {
    $data['aid'] =I('get.aid');
    $data['field'] =I('get.field');

    // 附表名称
    $cid  = D('Article')->where(array('aid'=>$data['aid']))->getField('category_cid');
    $cate = S('category');
    $cate =isset($cate[$cid])?$cate[$cid]:null;
    if(!$cate)
      $this->error('链接错误');
    $mid = $cate['model_mid'];
    $model = S('model');
    $model =isset($model[$mid])?$model[$mid]:null;
    if(!$model)
      $this->error('链接错误');
    $data['table']= 'article_'.$model['name'];
    // 文件
    $file = D('ArticleView')->get_field_by_aid($data);

    header("Content-type:application/octet-stream");//二进制文件
    $fileName = basename($file);//获得文件名
    header("Content-Disposition:attachment;filename={$fileName}");//下载窗口中显示的文件名
    header("Accept-ranges:bytes");//文件尺寸单位
    header("Accept-length:".filesize($file));//文件大小
    readfile($file);//读出文件内容
  }




}